package com.waiterxiaoyy.backandroiddesign.utils.poi;

import com.waiterxiaoyy.backandroiddesign.entity.Member;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/12/31 10:27
 */
public class MemberPoi {

    private static String filepath = "C:\\Users\\asus\\Desktop\\党员信息\\学生第三党支部.xlsx";
    public static List<Member> saveMember() throws IOException {
        InputStream inputStream = new FileInputStream(filepath);
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        int maxRowNum = sheet.getPhysicalNumberOfRows();
        List<Member> memberList = new ArrayList<>();
        for(int i = 1; i < maxRowNum; i++) {
            Member member = new Member();
            Row row = sheet.getRow(i);
            String name = getCellStringValue(row.getCell(0));
            String branch = getCellStringValue(row.getCell(1));
            String phone = getCellStringValue(row.getCell(13));
            member.setName(name);
            member.setBranch(branch);
            member.setPhone(phone);
            memberList.add(member);
        }
        return memberList;
    }
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream(filepath);
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        int maxRowNum = sheet.getPhysicalNumberOfRows();
        List<Member> memberList = new ArrayList<>();
        for(int i = 1; i < maxRowNum; i++) {
            Member member = new Member();
            Row row = sheet.getRow(i);
            String name = getCellStringValue(row.getCell(0));
            String branch = getCellStringValue(row.getCell(1));
            String phone = getCellStringValue(row.getCell(13));
            member.setName(name);
            member.setBranch(branch);
            member.setPhone(phone);
            memberList.add(member);
        }
    }

    public static String getCellStringValue(Cell cell) {
        if (cell == null) {
            return "";
        }
        int type = cell.getCellType();
        String cellValue;
        switch (type) {
            case 3:
                cellValue = "";
                break;
            case 5  :
                cellValue = "";
                break;
            case 4:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case 0:
                cellValue = NumberToTextConverter.toText(cell.getNumericCellValue());
                break;
            case 1:
                cellValue = cell.getStringCellValue();
                break;
            case 2:
                cellValue = cell.getCellFormula();
                break;
            default:
                cellValue = "";
                break;
        }
        return cellValue;
    }
}
